﻿using UnityEngine;

namespace Core.Audio
{
    [DisallowMultipleComponent]
    public class UIAudioSourcePlayer : MonoBehaviour
    {
        public EGameEvent PlayEvent;
        public EGameEvent StopEvent;

        //public AudioSource Audi;
        public AudioClip clip;

        private void Awake()
        {
            //var g = Sys.Ins.game.Curr;

            //g.Get<AudioSystem>();
            //EventInstance = RuntimeManager.CreateInstance(EventPath);

            AddFMODEvent(PlayEvent);
            AddFMODEvent(StopEvent);
        }



        private void AddFMODEvent(EGameEvent gEvent)
        {
            if (AudioEvent.Dict.ContainsKey(gEvent))
            {
                gameObject.AddComponent(AudioEvent.Dict[PlayEvent]);
            }
            else if (gEvent != EGameEvent.None)
            {
                log.err($"没有该类型的触发 {gEvent}");
            }
        }

        public void HandleGameEvent(EGameEvent gameEvent)
        {
            if (PlayEvent == gameEvent)
            {
                Play();
            }
            if (StopEvent == gameEvent)
            {
                Stop();
            }
        }


        private void Play()
        {
            //if (EventInstance.isValid())
            //{
            //    EventInstance.start();
            //}
        }

        private void Stop()
        {
            //if (EventInstance.isValid())
            //{
            //    EventInstance.stop(fs::STOP_MODE.ALLOWFADEOUT);
            //    EventInstance.release();
            //    EventInstance.clearHandle();
            //}
        }
    }
}
